﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.Model;
using MyOA.BLL;
namespace MyOA.UI.ScheduleManage.PersonSchedule
{
    public partial class PersonSchedule : System.Web.UI.Page
    {
        PersonScheduleManage manage = new PersonScheduleManage();
         
               
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                

                if (Session["loginId"]==null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                   
                }
                if (Request.QueryString["NodeValuePath"]==null)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }

                int nodeId;
                if (int.TryParse(Request.QueryString["NodeValuePath"], out nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                
                string loginId = (string)Session["loginId"];
                if (new CommandManage().isHaveRoleRight(loginId,nodeId)==false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                int userId = manage.getUserIdByLoginId(loginId);//获取登陆ID
                Session["userId"] = userId;//把登陆ID放在SESSION里 在本页面日历控件生成日期时调用

                DateTime dt = DateTime.Now;

                txtYear.Text = dt.Year.ToString();
                ddlMonth.SelectedValue = dt.Month.ToString();
                

              
            }
            
            
        }

        /// <summary>
        /// 日历控件选中日期发生改变时发生的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            Calendar1.SelectedDayStyle.ForeColor = System.Drawing.Color.Purple;
            Calendar1.SelectedDayStyle.CssClass = "SelectedDayStyle";
        }

        /// <summary>
        /// 绑定日历控件每个日期时发生的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {

            //获取每个单元格的日期
            CalendarDay calDay = e.Day;
            //获取表示呈现在控件中的单元格
            TableCell tc = e.Cell;
            //农历转换对象
            ChinaCalender dt = new ChinaCalender(calDay.Date);
            
            
            //不是本月的不显示
            if (calDay.IsOtherMonth)
            {
               tc.Controls.Clear();
            }
            else
            {
                
                HtmlAnchor a=  new HtmlAnchor();
                a.HRef = "SaveMySchedule.aspx?today=" + calDay.Date.ToShortDateString();

                HtmlImage image = new HtmlImage();
                image.Src = "~/images/add_Schedule.gif";

                a.Controls.Add(image);



                a.Attributes["onmouseover"] = "ddrivetip('新增个人日程',100)";
                a.Attributes["onmouseout"] = "hideddrivetip()";




                tc.Controls.Add(new LiteralControl("&nbsp;" + "&nbsp;" + "&nbsp;"));
                tc.Controls.Add(a);

                //在转农历时会发生错误 所以捕获异常
                try
                {

                    //显示农历
                    tc.Controls.Add(new LiteralControl("<br>" + dt.GetLunarHolDay()));
                   // tc.Controls.Add(new LiteralControl("<br>"+dt.()));


                }
                catch (Exception ex)
                {
                    //Response.Write(ex.ToString());
                }
                
                //添加本日数据库里有的超连接
                if (Session["userId"] != null)
                {
                    int userId = (int)Session["userId"];
                    Schedule schedule = null;

                    //获取个人日程
                    schedule = manage.getScheduleByCreateTimeAndCreateUserId(calDay.Date, userId);

                    if (schedule.Address!=string.Empty)
                    {
                        schedule.CreateUser.UserId = userId;
                        //把个人日程信息放入SESSION
                        Session["schedule"+calDay.Date.ToShortDateString()] = schedule;

                        //创建PANEL指定CssClass 来改变里面A标记的样式
                        Panel panel = new Panel();
                        panel.CssClass = "ServicePanel";
                        HtmlAnchor scheduleHref = new HtmlAnchor();

                        //穿过去的update参数无意义 只是判断是增加个人日程还是修改个人日程
                        scheduleHref.HRef = "SaveMySchedule.aspx?today=" + calDay.Date.ToShortDateString() + "&update=update";
                        string title = schedule.Title;
                        //判断标题长度 截取字符串
                        if (title.Length>5)
                        {
                            title=title.Substring(0, 5)+"...";
                        }
                        //a标签里的文字 是数据库里个人日程的TITLE
                        LiteralControl literalText = new LiteralControl(title);

                        scheduleHref.Controls.Add(literalText);

                        scheduleHref.Attributes["onmouseover"] = "ddrivetip('"+schedule.Title+"',100)";
                        scheduleHref.Attributes["onmouseout"] = "hideddrivetip()";

                        panel.Controls.Add(scheduleHref);

                        tc.Controls.Add(panel);
                    }
                }
                
                e.Cell.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';cursor='hand';";
                e.Cell.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#cef3ff';";

            }
        }

        protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            DateTime time = e.NewDate;
            txtYear.Text = time.Year.ToString();
            ddlMonth.SelectedValue = time.Month.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            
            int year = int.Parse(txtYear.Text);
            int month = int.Parse(ddlMonth.SelectedValue);
            DateTime dt = new DateTime(year, month,1);
            Calendar1.VisibleDate = dt;//日历控件上显示的年月更换
        }
    }
}
